package web;

import entity.Classroom;
import entity.Msg;
import entity.Reservation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import service.ClassroomService;

import javax.swing.*;
import java.util.Date;
import java.util.List;

/***
 * Author: Mr.Zhao, Time: 2019/5/24/17:07
 * Description:
 */
@Controller
public class ClassroomController {

    @Autowired
    ClassroomService classroomService;

    /**
     * 根据时间段以及教学楼查询教室空闲情况
     */
    @RequestMapping(value = "/classroom", method = RequestMethod.POST)
    @ResponseBody
    public Msg QueryByUser(@RequestParam(value = "start_time") Date startTime,
                           @RequestParam(value = "end_time") Date endTime,
                           @RequestParam(value = "building") Integer buildingId) {

        List<Classroom> classrooms = classroomService.selectFree(startTime, endTime, buildingId);
        return Msg.success().add("classrooms", classrooms);
    }


    //TODO: 想要在controller层处理异常以便返回异常数据
    @RequestMapping(value = "/classroom", method = RequestMethod.POST, params = {"start_time", "end_time"})
    @ResponseBody
    @ExceptionHandler(RuntimeException.class)
    public Msg queryFree(@RequestParam(value = "start_time") String startTime,
                         @RequestParam(value = "end_time") String endTime) {
        Date start = new Date(Long.parseLong(startTime));
        Date end = new Date(Long.parseLong(endTime));
        return Msg.success().add("buildings", classroomService.selectFree(start, end));
    }

    /**
     * 查询所有教室
     * @return
     */
    @RequestMapping(value = "/classroom",method = RequestMethod.GET)
    @ResponseBody
    public Msg queryAll()
    {
        try {
            return Msg.success().add("buildings", classroomService.selectAll());
        }catch (Exception e) {
            return Msg.fail().setMsg("查询失败！");
        }
    }
}
